using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MobileTDL
{
	public partial class TaskForm : Form
	{
		private ToDoTask _task;

		public TaskForm()
		{
			InitializeComponent();
		}

		public ToDoTask Task
		{
			get { return _task; }
			set
			{
				_task = value;
				if (_task != null)
				{
					cboRisk.Text = _task.Risk;
					txtAllocatedBy.Text = _task.AllocatedBy;
					txtCategory.Text = _task.Category;
					txtComments.Text = _task.Comments;
					txtPerson.Text = _task.Person;
					txtStatus.Text = _task.Status;
					txtTitle.Text = _task.Title;

                    chkCompleted.Checked = _task.Completed;
					ntxtPercentageComplete.Value = (_task.PercentDone.HasValue ? _task.PercentDone.Value : 0);

					if (_task.Priority.HasValue) cboPriority.Text = _task.Priority.ToString();

                    if (_task.CreationDate.HasValue) dtCreated.Value = _task.CreationDate.Value;
                    else dtCreated.Value = dtCreated.MinDate;

					if (_task.DoneDate.HasValue) dtDone.Value = _task.DoneDate.Value;
					else dtDone.Value = dtDone.MinDate;

					if (_task.DueDate.HasValue) dtDueDate.Value = _task.DueDate.Value;
					else dtDueDate.Value = dtDone.MinDate;

					if (_task.StartDate.HasValue) dtStartDate.Value = _task.StartDate.Value;
					else dtStartDate.Value = dtDone.MinDate;

					if (_task.EarliestDueDate.HasValue) dtEarliestDueDate.Value = _task.EarliestDueDate.Value;
					else dtEarliestDueDate.Value = dtDone.MinDate;
				}
				else
				{
                    // Passed taks is null. Initialize with default values.

					dtCreated.Value = dtCreated.MinDate;
					dtDone.Value = dtDone.MinDate;
					dtDueDate.Value = dtDueDate.MinDate;
					dtEarliestDueDate.Value = dtEarliestDueDate.MinDate;
					dtStartDate.Value = dtStartDate.MinDate;
					cboPriority.Text = "";
					cboRisk.Text = "";
					txtAllocatedBy.Text = "";
					txtCategory.Text = "";
					txtComments.Text = "";
					txtPerson.Text = "";
					txtStatus.Text = "";
					txtTitle.Text = "";
                    chkCompleted.Checked = false;
					ntxtPercentageComplete.Value = 0;
				}

				bool enabled = (_task != null);
				dtCreated.Enabled = enabled;
				dtDone.Enabled = enabled;
				dtDueDate.Enabled = enabled;
				dtStartDate.Enabled = enabled;
				cboPriority.Enabled = enabled;
				cboRisk.Enabled = enabled;
				txtAllocatedBy.Enabled = enabled;
				txtCategory.Enabled = enabled;
				txtComments.Enabled = enabled;
				txtPerson.Enabled = enabled;
				txtStatus.Enabled = enabled;
				txtTitle.Enabled = enabled;
                // chkCompleted.Enabled = enabled;
				ntxtPercentageComplete.Enabled = enabled;

                // Calculated field will remain disabled.
                chkCompleted.Enabled = false;
			}
		}

        /// <summary>
        /// Save task.
        /// </summary>
		private void Save()
		{
			if (Task != null)
			{
				if(dtCreated.Value != dtCreated.MinDate) Task.CreationDate = dtCreated.Value;
                if (dtDone.Value != dtDone.MinDate) Task.DoneDate = dtDone.Value;
                if (dtDueDate.Value != dtDueDate.MinDate) Task.DueDate = dtDueDate.Value;
				if(dtStartDate.Value != dtStartDate.MinDate) Task.StartDate = dtStartDate.Value;
				Task.AllocatedBy = txtAllocatedBy.Text;
				Task.Category = txtCategory.Text;
				Task.Comments = txtComments.Text;
				Task.Person = txtPerson.Text;
				// Task.Priority = (int?)cboPriority.SelectedItem;
                Task.Priority = int.Parse(cboPriority.SelectedItem.ToString());
				Task.Risk = cboRisk.Text;
				Task.Status = txtStatus.Text;
				Task.Title = txtTitle.Text;
				if(dtEarliestDueDate.Value != dtEarliestDueDate.MinDate) Task.EarliestDueDate = dtEarliestDueDate.Value;

                // Task.Completed = chkCompleted.Checked;
				Task.PercentDone = (int?)ntxtPercentageComplete.Value;
			}
		}

		private void txt_GotFocus(object sender, EventArgs e)
		{
			inputPanel1.Enabled = true;
		}

		private void txt_LostFocus(object sender, EventArgs e)
		{
			inputPanel1.Enabled = false;
		}

		private void miOK_Click(object sender, EventArgs e)
		{
			Save();
			DialogResult = DialogResult.OK;
		}

		private void miMenuCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
		}

		private Control GetFocusedControl(ControlCollection controls)
		{
			Control focusedControl = null;
			foreach (Control control in controls)
			{
				if (control.Focused) focusedControl = control;
				else if (control.Controls.Count > 0) focusedControl = GetFocusedControl(control.Controls);
				if (focusedControl != null) break;
			}
			return focusedControl;
		}

		private void miMenuCut_Click(object sender, EventArgs e)
		{
			Control focusedControl = GetFocusedControl(this.Controls);
			if (focusedControl is TextBox)
			{
				TextBox txt = (TextBox)focusedControl;
				int selectionStart = txt.SelectionStart;
				Clipboard.SetDataObject(txt.SelectedText);
				txt.Text = txt.Text.Remove(selectionStart, txt.SelectionLength);
				txt.SelectionStart = selectionStart;
			}
		}

		private void miMenuCopy_Click(object sender, EventArgs e)
		{
			Control focusedControl = GetFocusedControl(this.Controls);
			if (focusedControl is TextBox)
			{
				TextBox txt = (TextBox)focusedControl;
				Clipboard.SetDataObject(txt.SelectedText);
			}
		}

		private void miMenuPaste_Click(object sender, EventArgs e)
		{
			Control focusedControl = GetFocusedControl(this.Controls);
			if (focusedControl is TextBox)
			{
				TextBox txt = (TextBox)focusedControl;
				int selectionStart = txt.SelectionStart;
				if (txt.SelectionLength > 0) txt.Text = txt.Text.Remove(txt.SelectionStart, txt.SelectionLength);
				string clipboardText = (string)Clipboard.GetDataObject().GetData(DataFormats.Text);
				txt.Text = txt.Text.Insert(txt.SelectionStart, clipboardText);
				txt.SelectionStart = selectionStart + clipboardText.Length;
			}
		}

		private void miMenuSelectAll_Click(object sender, EventArgs e)
		{
			Control focusedControl = GetFocusedControl(this.Controls);
			if (focusedControl is TextBox)
			{
				TextBox txt = (TextBox)focusedControl;
				txt.SelectAll();
			}
		}

		private void miMenuUndo_Click(object sender, EventArgs e)
		{
			Control focusedControl = GetFocusedControl(this.Controls);
			if (focusedControl is TextBox)
			{
				TextBox txt = (TextBox)focusedControl;
				txt.Undo();
			}
		}
	}
}